<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"kuang-yudi.gitee.io","root":"/myblog/","images":"/myblog/images","scheme":"Gemini","version":"8.1.0","exturl":false,"sidebar":{"position":"right","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":null,"activeClass":"gitalk"},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/myblog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="数组去重">
<meta property="og:type" content="article">
<meta property="og:title" content="数组去重">
<meta property="og:url" content="http://kuang-yudi.gitee.io/myblog/2021/01/14/%E6%95%B0%E7%BB%84%E5%8E%BB%E9%87%8D/index.html">
<meta property="og:site_name" content="KY迪的个人博客">
<meta property="og:description" content="数组去重">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-01-14T13:08:41.000Z">
<meta property="article:modified_time" content="2021-05-05T07:52:29.184Z">
<meta property="article:author" content="匡宇迪">
<meta property="article:tag" content="数组去重">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://kuang-yudi.gitee.io/myblog/2021/01/14/%E6%95%B0%E7%BB%84%E5%8E%BB%E9%87%8D/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>数组去重 | KY迪的个人博客</title>
  



  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">KY迪的个人博客</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">个人技术博客</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-categories"><a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-archives"><a href="/myblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <section class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%B5%8C%E5%A5%97-for-%E5%BE%AA%E7%8E%AF%E7%BB%93%E5%90%88-splice-%E6%96%B9%E6%B3%95%EF%BC%88%E6%94%B9%E5%8F%98%E5%8E%9F%E6%95%B0%E7%BB%84%EF%BC%89"><span class="nav-number">1.</span> <span class="nav-text">嵌套 for 循环结合 splice 方法（改变原数组）</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%9D%E8%B7%AF%EF%BC%9A"><span class="nav-number">1.1.</span> <span class="nav-text">思路：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BC%98%E7%82%B9%EF%BC%9A"><span class="nav-number">1.2.</span> <span class="nav-text">优点：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BC%BA%E7%82%B9%EF%BC%9A"><span class="nav-number">1.3.</span> <span class="nav-text">缺点：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%BB%BA%E6%96%B0%E6%95%B0%E7%BB%84%EF%BC%8C%E5%88%A9%E7%94%A8-indexOf-%E5%8E%BB%E9%87%8D"><span class="nav-number">2.</span> <span class="nav-text">建新数组，利用 indexOf 去重</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%9D%E8%B7%AF"><span class="nav-number">2.1.</span> <span class="nav-text">思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BC%98%E7%82%B9%EF%BC%9A-1"><span class="nav-number">2.2.</span> <span class="nav-text">优点：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BC%BA%E7%82%B9%EF%BC%9A-1"><span class="nav-number">2.3.</span> <span class="nav-text">缺点：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#ES6-%E4%B8%AD%E5%88%A9%E7%94%A8-Set-%E5%8E%BB%E9%87%8D"><span class="nav-number">3.</span> <span class="nav-text">ES6 中利用 Set 去重</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%9D%E8%B7%AF%EF%BC%9A-1"><span class="nav-number">3.1.</span> <span class="nav-text">思路：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BC%98%E7%82%B9%EF%BC%9A-2"><span class="nav-number">3.2.</span> <span class="nav-text">优点：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BC%BA%E7%82%B9%EF%BC%9A-2"><span class="nav-number">3.3.</span> <span class="nav-text">缺点：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#includes-%E5%8E%BB%E9%87%8D-%E8%BF%94%E5%9B%9E%E6%96%B0%E6%95%B0%E7%BB%84"><span class="nav-number">4.</span> <span class="nav-text">includes 去重 返回新数组</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%9D%E8%B7%AF-1"><span class="nav-number">4.1.</span> <span class="nav-text">思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BC%98%E7%82%B9%EF%BC%9A-3"><span class="nav-number">4.2.</span> <span class="nav-text">优点：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BC%BA%E7%82%B9%EF%BC%9A-3"><span class="nav-number">4.3.</span> <span class="nav-text">缺点：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%B6%E5%AE%83%E6%96%B9%E6%B3%95%E4%B8%AD%E8%83%BD%E5%BE%AA%E7%8E%AF%E6%95%B0%E7%BB%84%E9%83%BD%E5%8F%AF%E4%BB%A5%E7%BB%93%E5%90%88-includes"><span class="nav-number">5.</span> <span class="nav-text">其它方法中能循环数组都可以结合 includes</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%88%A9%E7%94%A8%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%B1%9E%E6%80%A7-key-%E7%9A%84%E5%94%AF%E4%B8%80%E6%80%A7%E5%8E%BB%E9%87%8D"><span class="nav-number">6.</span> <span class="nav-text">利用对象的属性 key 的唯一性去重</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%9D%E8%B7%AF-2"><span class="nav-number">6.1.</span> <span class="nav-text">思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BC%98%E7%82%B9%EF%BC%9A-4"><span class="nav-number">6.2.</span> <span class="nav-text">优点：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BC%BA%E7%82%B9%EF%BC%9A-4"><span class="nav-number">6.3.</span> <span class="nav-text">缺点：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#ES6-%E4%B8%AD%E5%88%A9%E7%94%A8-Map-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E7%9A%84%E7%89%B9%E6%80%A7%E5%8E%BB%E9%87%8D"><span class="nav-number">7.</span> <span class="nav-text">ES6 中利用 Map 数据结构的特性去重</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%9D%E8%B7%AF-3"><span class="nav-number">7.1.</span> <span class="nav-text">思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BC%98%E7%82%B9%EF%BC%9A-5"><span class="nav-number">7.2.</span> <span class="nav-text">优点：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BC%BA%E7%82%B9%EF%BC%9A-5"><span class="nav-number">7.3.</span> <span class="nav-text">缺点：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#sort-%E6%96%B9%E6%B3%95%E5%8E%BB%E9%87%8D"><span class="nav-number">8.</span> <span class="nav-text">sort 方法去重</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%9D%E8%B7%AF-4"><span class="nav-number">8.1.</span> <span class="nav-text">思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BC%BA%E7%82%B9%EF%BC%9A-6"><span class="nav-number">8.2.</span> <span class="nav-text">缺点：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#sort-%E6%96%B9%E6%B3%95%E7%BB%93%E5%90%88-reduce-%E6%96%B9%E6%B3%95%E5%8E%BB%E9%87%8D"><span class="nav-number">9.</span> <span class="nav-text">sort 方法结合 reduce 方法去重</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%9D%E8%B7%AF-5"><span class="nav-number">9.1.</span> <span class="nav-text">思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BC%BA%E7%82%B9%EF%BC%9A-7"><span class="nav-number">9.2.</span> <span class="nav-text">缺点：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E5%AF%B9%E8%B1%A1%E5%8E%BB%E9%87%8D"><span class="nav-number">10.</span> <span class="nav-text">数组对象去重</span></a></li></ol></div>
        </section>
        <!--/noindex-->

        <section class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="匡宇迪"
      src="https://static.pptstore.net/picture/19/19/e1feb44f42cdf7e1202b_s.jpg">
  <p class="site-author-name" itemprop="name">匡宇迪</p>
  <div class="site-description" itemprop="description">用于记录学习web前端</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/myblog/archives/">
        
          <span class="site-state-item-count">38</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/myblog/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/myblog/tags/">
          
        <span class="site-state-item-count">87</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/tonykyd/tonykyd.github.io" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tonykyd&#x2F;tonykyd.github.io" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1303665509@qq.com" title="E-Mail → mailto:1303665509@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



        </section>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://kuang-yudi.gitee.io/myblog/2021/01/14/%E6%95%B0%E7%BB%84%E5%8E%BB%E9%87%8D/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://static.pptstore.net/picture/19/19/e1feb44f42cdf7e1202b_s.jpg">
      <meta itemprop="name" content="匡宇迪">
      <meta itemprop="description" content="用于记录学习web前端">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="KY迪的个人博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          数组去重
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-14 21:08:41" itemprop="dateCreated datePublished" datetime="2021-01-14T21:08:41+08:00">2021-01-14</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-05-05 15:52:29" itemprop="dateModified" datetime="2021-05-05T15:52:29+08:00">2021-05-05</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/myblog/categories/%E9%9D%A2%E8%AF%95%E9%A2%98/" itemprop="url" rel="index"><span itemprop="name">面试题</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

            <div class="post-description">数组去重</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="嵌套-for-循环结合-splice-方法（改变原数组）"><a href="#嵌套-for-循环结合-splice-方法（改变原数组）" class="headerlink" title="嵌套 for 循环结合 splice 方法（改变原数组）"></a>嵌套 for 循环结合 splice 方法（改变原数组）</h1><h2 id="思路："><a href="#思路：" class="headerlink" title="思路："></a>思路：</h2><p>从起始位置遍历数组，同时从下一位开始遍历，比较两个位置的元素，如果一样用 splice 方法删除，同时第二次的位置减一(因为删除后下一项自动取代了删除项的位置)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">function newArr(arr)&#123;</span><br><span class="line">    for(var i&#x3D;0;i&lt;arr.length;i++)&#123;</span><br><span class="line">        for(var j&#x3D;i+1;j&lt;arr.length;j++)&#123;</span><br><span class="line">            if(arr[i]&#x3D;&#x3D;&#x3D;arr[j])&#123;</span><br><span class="line">              &#x2F;&#x2F;如果第一个等于第二个，splice方法删除第二个</span><br><span class="line">              arr.splice(j,1);</span><br><span class="line">              j--;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return arr;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">var arr &#x3D; [1,1,2,5,6,3,5,5,6,8,9,8];</span><br><span class="line">console.log(newArr(arr))</span><br></pre></td></tr></table></figure>

<h2 id="优点："><a href="#优点：" class="headerlink" title="优点："></a>优点：</h2><p>该方法可以过滤掉重复的 String、Boolean、 Number、undefined、null，返回的是去重后的原数组。</p>
<h2 id="缺点："><a href="#缺点：" class="headerlink" title="缺点："></a>缺点：</h2><p>不能过滤掉 NaN、Object,嵌套 for 循环比较耗内存(外层遍历一项内层都要遍历所有项)</p>
<h1 id="建新数组，利用-indexOf-去重"><a href="#建新数组，利用-indexOf-去重" class="headerlink" title="建新数组，利用 indexOf 去重"></a>建新数组，利用 indexOf 去重</h1><h2 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h2><p>新建一个数组，原数组遍历传入新数组，indexOf 方法判断值是否存在新数组中，值不存在就加入该新数组中。<br>方法“indexOf”是 es5 的方法，IE8 以下不支持。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">function newArr(array)&#123;</span><br><span class="line">    &#x2F;&#x2F;一个新的数组</span><br><span class="line">    var arrs &#x3D; [];</span><br><span class="line">    &#x2F;&#x2F;遍历当前数组</span><br><span class="line">    for(var i &#x3D; 0; i &lt; array.length; i++)&#123;</span><br><span class="line">        &#x2F;&#x2F;如果临时数组里没有当前数组的当前值，则把当前值push到新数组里面</span><br><span class="line">        if (arrs.indexOf(array[i]) &#x3D;&#x3D;&#x3D; -1)&#123;</span><br><span class="line">            arrs.push(array[i])</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">    return arrs;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">var arr &#x3D; [1,1,2,5,5,6,8,9,8];</span><br><span class="line">console.log(newArr(arr))</span><br></pre></td></tr></table></figure>

<h2 id="优点：-1"><a href="#优点：-1" class="headerlink" title="优点："></a>优点：</h2><p>该方法可以过滤重复的 String、Boolean、 Number、undefined、null，返回的是去重后的新数组。</p>
<h2 id="缺点：-1"><a href="#缺点：-1" class="headerlink" title="缺点："></a>缺点：</h2><p>不能过滤掉 NaN、Object，占用内存消耗大(js 引擎在实现这个方法的时候会遍历数组直到找到目标为止。所以此函数会浪费掉很多时间)</p>
<h1 id="ES6-中利用-Set-去重"><a href="#ES6-中利用-Set-去重" class="headerlink" title="ES6 中利用 Set 去重"></a>ES6 中利用 Set 去重</h1><h2 id="思路：-1"><a href="#思路：-1" class="headerlink" title="思路："></a>思路：</h2><p>Set 集合数据结构（原型对象为 Object，可遍历），类似数组但没有重复值，Array.from 方法可以将伪数组或可遍历对象转换为数组。或者用扩展运算符代替 Array.from 方法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">function newArr(arr)&#123;</span><br><span class="line">    return Array.from(new Set(arr))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">var arr &#x3D; [1,1,2,9,6,9,6,3,1,4,5];</span><br><span class="line">console.log(newArr(arr))</span><br></pre></td></tr></table></figure>

<h2 id="优点：-2"><a href="#优点：-2" class="headerlink" title="优点："></a>优点：</h2><p>该方法可以顾虑到重复的 String、Boolean、<br>Number、undefined、null、NaN，返回的是去重后的新数组。</p>
<h2 id="缺点：-2"><a href="#缺点：-2" class="headerlink" title="缺点："></a>缺点：</h2><p>不能过滤重复的 Object。</p>
<h1 id="includes-去重-返回新数组"><a href="#includes-去重-返回新数组" class="headerlink" title="includes 去重 返回新数组"></a>includes 去重 返回新数组</h1><h2 id="思路-1"><a href="#思路-1" class="headerlink" title="思路"></a>思路</h2><p>includes() 方法判断一个数组是否包含一个指定的值返回布尔值</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">let arr &#x3D; [1,1,&#39;true&#39;,&#39;true&#39;,true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,&#39;NaN&#39;,&#39;NaN&#39;, 0, 0, &#39;a&#39;, &#39;a&#39;,&#123;&#125;,&#123;&#125;];</span><br><span class="line"></span><br><span class="line">   function distinct (arr) &#123;</span><br><span class="line">       let newArr &#x3D; []</span><br><span class="line">       for(let i &#x3D; 0; i &lt; arr.length; i++) &#123;</span><br><span class="line">           if(!newArr.includes(arr[i])) &#123;</span><br><span class="line">               newArr.push(arr[i])</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       return newArr</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   console.log(distinct(arr)) &#x2F;&#x2F; [1, &quot;true&quot;, true, 15, false, undefined, null, NaN, &quot;NaN&quot;, 0, &quot;a&quot;, &#123;…&#125;, &#123;…&#125;]</span><br></pre></td></tr></table></figure>

<h2 id="优点：-3"><a href="#优点：-3" class="headerlink" title="优点："></a>优点：</h2><p>该方法可以顾虑到重复的 String、Boolean、Number、undefined、null、NaN，返回的是去重后的新数组。</p>
<h2 id="缺点：-3"><a href="#缺点：-3" class="headerlink" title="缺点："></a>缺点：</h2><p>不能过滤掉 Object，消耗内存</p>
<h1 id="其它方法中能循环数组都可以结合-includes"><a href="#其它方法中能循环数组都可以结合-includes" class="headerlink" title="其它方法中能循环数组都可以结合 includes"></a>其它方法中能循环数组都可以结合 includes</h1><p>forEach、filter</p>
<p>例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">function unique6(arr) &#123;</span><br><span class="line">            var newArr &#x3D; []</span><br><span class="line">            newArr &#x3D; arr.filter(function (item) &#123;</span><br><span class="line">                return newArr.includes(item) ? &#39;&#39; : newArr.push(item)</span><br><span class="line">            &#125;)</span><br><span class="line">            return newArr</span><br><span class="line">        &#125;</span><br><span class="line">        console.log(unique6([1, 1, 2, 3, 5, 3, 1, 5, 6, 7, 4]));</span><br><span class="line">        &#x2F;&#x2F; 结果是[1, 2, 3, 5, 6, 7, 4]</span><br></pre></td></tr></table></figure>

<h1 id="利用对象的属性-key-的唯一性去重"><a href="#利用对象的属性-key-的唯一性去重" class="headerlink" title="利用对象的属性 key 的唯一性去重"></a>利用对象的属性 key 的唯一性去重</h1><h2 id="思路-2"><a href="#思路-2" class="headerlink" title="思路"></a>思路</h2><p>将数组的每一项作为对象的 key，遍历数组，如果对象中没有与此项相同的属性 key ，则将此项添加为对象的属性同时添加到空数组中。</p>
<p><strong>注意：对象的属性名只能为基本数据类型，如果为引用类型会变成字符串</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">let arr &#x3D; [1,1,&#39;true&#39;,&#39;true&#39;,true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,&#39;NaN&#39;,&#39;NaN&#39;, 0, 0, &#39;a&#39;, &#39;a&#39;,&#123;&#125;,&#123;&#125;];</span><br><span class="line"></span><br><span class="line">    function distinct(arr) &#123;</span><br><span class="line">        let obj &#x3D; &#123;&#125;</span><br><span class="line">        let newArr &#x3D; []</span><br><span class="line">        for (let i &#x3D; 0; i &lt; arr.length; i++) &#123;</span><br><span class="line">          if (!obj[typeof arr[i] + arr[i]]) &#123;</span><br><span class="line">            obj[typeof arr[i] + arr[i]] &#x3D; 1</span><br><span class="line">            newArr.push(arr[i])</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        return newArr</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    console.log(distinct(arr)) &#x2F;&#x2F; [1, &quot;true&quot;, true, &quot;15&quot;, 15, false, undefined, null, NaN, &quot;NaN&quot;, 0, &quot;a&quot;, &#123;…&#125;]</span><br></pre></td></tr></table></figure>

<h2 id="优点：-4"><a href="#优点：-4" class="headerlink" title="优点："></a>优点：</h2><p>该方法可以过滤重复的 String、Boolean、<br>Number、undefined、null、NaN、Object，返回的是去重后的原数组。</p>
<h2 id="缺点：-4"><a href="#缺点：-4" class="headerlink" title="缺点："></a>缺点：</h2><p>对象的属性是字符串类型的，即本身数字 1 和字符串‘1’是不同的，但保存到对象中时会发生隐式类型转换，导致去重存在一定的隐患(数字 1 和字符串 1 作为属性时都会转化成字符串)。所以在保存为属性时让他们不一样。</p>
<h1 id="ES6-中利用-Map-数据结构的特性去重"><a href="#ES6-中利用-Map-数据结构的特性去重" class="headerlink" title="ES6 中利用 Map 数据结构的特性去重"></a>ES6 中利用 Map 数据结构的特性去重</h1><h2 id="思路-3"><a href="#思路-3" class="headerlink" title="思路"></a>思路</h2><p>Map 数据结构：类似对象的键值对集合,将数组中的每项作为 Map 数据的键保存，通过 map.has 方法判断是否已经存在。</p>
<p><strong>结合对象的属性 key 的唯一性原理以及 Map 中的属性可以是任何数据类型</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">let arr &#x3D; [1,1,&#39;true&#39;,&#39;true&#39;,true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,&#39;NaN&#39;,&#39;NaN&#39;, 0, 0, &#39;a&#39;, &#39;a&#39;,&#123;&#125;,&#123;&#125;];</span><br><span class="line">    function distinct(arr) &#123;</span><br><span class="line">        let map &#x3D; new Map()</span><br><span class="line">        let newArr &#x3D; []</span><br><span class="line">        for(let i &#x3D; 0; i &lt; arr.length; i++) &#123;</span><br><span class="line">            if(!map.has(arr[i])) &#123;</span><br><span class="line">                map.set(arr[i])</span><br><span class="line">                newArr.push(arr[i])</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        return newArr</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    console.log(distinct(arr)) &#x2F;&#x2F; [1, &quot;true&quot;, true, 15, false, undefined, null, NaN, &quot;NaN&quot;, 0, &quot;a&quot;, &#123;…&#125;, &#123;…&#125;]</span><br></pre></td></tr></table></figure>

<h2 id="优点：-5"><a href="#优点：-5" class="headerlink" title="优点："></a>优点：</h2><p>该方法可以过滤重复的 String、Boolean、<br>Number、undefined、null、NaN，返回的是去重后的新数组。</p>
<h2 id="缺点：-5"><a href="#缺点：-5" class="headerlink" title="缺点："></a>缺点：</h2><p>不能过滤重复的 Object。</p>
<h1 id="sort-方法去重"><a href="#sort-方法去重" class="headerlink" title="sort 方法去重"></a>sort 方法去重</h1><h2 id="思路-4"><a href="#思路-4" class="headerlink" title="思路"></a>思路</h2><p>先将原数组排序，在与相邻的进行比较，如果不同则存入新数组。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">let arr &#x3D; [1,1,&#39;true&#39;,&#39;true&#39;,true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,&#39;NaN&#39;,&#39;NaN&#39;, 0, 0, &#39;a&#39;, &#39;a&#39;,&#123;&#125;,&#123;&#125;];</span><br><span class="line">    function unique2(arr) &#123;</span><br><span class="line">            var formArr &#x3D; arr.sort()</span><br><span class="line">            var newArr&#x3D;[formArr[0]]</span><br><span class="line">            for (let i &#x3D; 1; i &lt; formArr.length; i++) &#123;</span><br><span class="line">                if (formArr[i]!&#x3D;&#x3D;formArr[i-1]) &#123;</span><br><span class="line">                    newArr.push(formArr[i])</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            return newArr</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    console.log(unique2(arr)) &#x2F;&#x2F; [0, 1, 15, NaN, NaN, &quot;NaN&quot;, &#123;…&#125;, &#123;…&#125;, &quot;a&quot;, false, null, &quot;true&quot;, true, undefined]</span><br></pre></td></tr></table></figure>

<h2 id="缺点：-6"><a href="#缺点：-6" class="headerlink" title="缺点："></a>缺点：</h2><p>不能去重 NaN,Object</p>
<h1 id="sort-方法结合-reduce-方法去重"><a href="#sort-方法结合-reduce-方法去重" class="headerlink" title="sort 方法结合 reduce 方法去重"></a>sort 方法结合 reduce 方法去重</h1><h2 id="思路-5"><a href="#思路-5" class="headerlink" title="思路"></a>思路</h2><p>先用 sort 方法排序，再用 reduce 方法给一个初始空数组，将原数组第一项放进去后与循环时的当前项比较，如果不同再放进去</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">let arr &#x3D; [1,1,&#39;true&#39;,&#39;true&#39;,true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,&#39;NaN&#39;,&#39;NaN&#39;, 0, 0, &#39;a&#39;, &#39;a&#39;,&#123;&#125;,&#123;&#125;];</span><br><span class="line">    function distinct(arr) &#123;</span><br><span class="line">        return arr.sort().reduce((init, current) &#x3D;&gt; &#123;</span><br><span class="line">            if(init.length &#x3D;&#x3D;&#x3D; 0 || init[init.length-1] !&#x3D;&#x3D; current) &#123;</span><br><span class="line">                init.push(current);</span><br><span class="line">            &#125;</span><br><span class="line">            return init;</span><br><span class="line">        &#125;, []);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    console.log(distinct(arr)) &#x2F;&#x2F; [0, 1, 15, NaN, NaN, &quot;NaN&quot;, &#123;…&#125;, &#123;…&#125;, &quot;a&quot;, false, null, &quot;true&quot;, true, undefined]</span><br></pre></td></tr></table></figure>

<h2 id="缺点：-7"><a href="#缺点：-7" class="headerlink" title="缺点："></a>缺点：</h2><p>不能过滤重复的 NaN 和 Object</p>
<h1 id="数组对象去重"><a href="#数组对象去重" class="headerlink" title="数组对象去重"></a>数组对象去重</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 2. 数组对象去重——reduce遍历+对象属性唯一性</span><br><span class="line">    let arrObj &#x3D; [</span><br><span class="line">      &#123; name: &#39;zs&#39; &#125;,</span><br><span class="line">      &#123; name: &#39;zs&#39; &#125;,</span><br><span class="line">      &#123; name: &#39;zs&#39; &#125;,</span><br><span class="line">      &#123; name: &#39;ls&#39; &#125;</span><br><span class="line">    ]</span><br><span class="line">    function handleArr2(arr) &#123;</span><br><span class="line">      let newObj &#x3D; &#123;&#125;</span><br><span class="line">      let arr1 &#x3D; arr.reduce((pre, cur) &#x3D;&gt; &#123;</span><br><span class="line">        &#x2F;&#x2F; newObj[cur.name] ? &#39;&#39; : newObj[cur.name] &#x3D; pre.push(cur)</span><br><span class="line">        if (!newObj[cur.name]) &#123;</span><br><span class="line">          newObj[cur.name] &#x3D; 1</span><br><span class="line">          pre.push(cur)</span><br><span class="line">        &#125;</span><br><span class="line">        return pre</span><br><span class="line">      &#125;, [])</span><br><span class="line">      return arr1</span><br><span class="line">    &#125;</span><br><span class="line">    let arr3 &#x3D; handleArr2(arrObj)</span><br><span class="line">    console.log(arr3);</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="followme">
  <span>欢迎关注我的其它发布渠道</span>

  <div class="social-list">

      <div class="social-item">
        <a target="_blank" class="social-link" href="/myblog/images/wechat_channel.jpg">
          <span class="icon">
            <i class="fab fa-weixin"></i>
          </span>

          <span class="label">WeChat</span>
        </a>
      </div>
  </div>
</div>

          <div class="post-tags">
              <a href="/myblog/tags/%E6%95%B0%E7%BB%84%E5%8E%BB%E9%87%8D/" rel="tag"># 数组去重</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/myblog/2021/01/14/%E9%97%AD%E5%8C%85/" rel="prev" title="闭包">
                  <i class="fa fa-chevron-left"></i> 闭包
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/myblog/2021/01/15/call%E3%80%81bind%E3%80%81apply%E6%96%B9%E6%B3%95/" rel="next" title="call、bind、apply方法">
                  call、bind、apply方法 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">匡宇迪</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/myblog/js/utils.js"></script><script src="/myblog/js/motion.js"></script><script src="/myblog/js/next-boot.js"></script>

  
<script src="/myblog/js/local-search.js"></script>






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>






<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1.7.0/dist/gitalk.css">

<script>
NexT.utils.loadComments('#gitalk-container', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1.7.0/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '1926f8a1c453a1121d8d',
      clientSecret: '426722f9eef7599f8ebb3220379e704ea007a5f2',
      repo        : '',
      owner       : '',
      admin       : [''],
      id          : 'f6e9542e64abbeb200a4282e696c7708',
        language: '',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

</body>
</html>
